<template>
  <div class="role-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>用户角色管理</span>
          <el-button type="primary" @click="handleAdd">新增角色</el-button>
        </div>
      </template>

      <el-table :data="roleList" border style="width: 100%">
        <el-table-column prop="ruId" label="ID" width="80" />
        <el-table-column prop="userId" label="用户ID" width="120" />
        <el-table-column prop="roleId" label="角色ID" width="120" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog v-model="dialogVisible" :title="dialogTitle">
        <el-form :model="form" label-width="120px">
          <el-form-item label="用户ID">
            <el-input v-model="form.userId" />
          </el-form-item>
          <el-form-item label="角色ID">
            <el-input v-model="form.roleId" />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="handleSubmit">确定</el-button>
          </span>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { queryAllRole, addRole, updateRole, deleteRole } from '@/api/role'

const roleList = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const form = ref({
  ruId: undefined,
  userId: '',
  roleId: ''
})

// 加载数据
const loadData = async () => {
  try {
    const res = await queryAllRole()
    roleList.value = res.data
  } catch (error) {
    ElMessage.error('加载失败')
  }
}

// 新增
const handleAdd = () => {
  form.value = {
    ruId: undefined,
    userId: '',
    roleId: ''
  }
  dialogTitle.value = '新增角色'
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row) => {
  form.value = { ...row }
  dialogTitle.value = '编辑角色'
  dialogVisible.value = true
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该角色？', '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      await deleteRole(row.ruId)
      ElMessage.success('删除成功')
      loadData()
    } catch (error) {
      ElMessage.error('删除失败')
    }
  })
}

// 提交表单
const handleSubmit = async () => {
  try {
    if (form.value.ruId) {
      await updateRole(form.value)
      ElMessage.success('更新成功')
    } else {
      await addRole(form.value)
      ElMessage.success('添加成功')
    }
    dialogVisible.value = false
    loadData()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.role-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  margin-top: 20px;
}
</style>
